package com.woniu.tool;

import com.woniu.dao.ScoreCardDao;
import com.woniu.entity.CreExamineNew;
import com.woniu.entity.ScoreCard;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.List;

/**
 * 2022-9-30 09:07:23
 * Wei.SJ
 * 评分引擎
 */
@Component
public class ScoreCardUtil {

    @Autowired
    private ScoreCardDao scoreCardDao;

    /**
     * 得到属性数组对象
     *
     * @param e CreExamine对象
     * @return
     * @throws Exception
     */
    public Field[] getFields(CreExamineNew e) throws Exception {
        Class cls = e.getClass();
        return cls.getDeclaredFields();
    }

    /**
     * 获取评分
     *
     * @param e
     * @return
     */
    public Integer getScore(CreExamineNew e) {
        int score = 0;
        List<ScoreCard> scoreList = scoreCardDao.findScoreList();
        try {
            Field[] fields = getFields(e);
            for (int i = 0; i < fields.length; i++) {
                Field f = fields[i];
                f.setAccessible(true);
                // 属性名
                String name = f.getName();
                // 属性值
                Object value = f.get(e);
                // 属性类型
                String typeName = f.getGenericType().getTypeName();
                // 遍历评分表
                for (ScoreCard item : scoreList) {
                    // 判断属性值类型
                    if (typeName.equals("java.lang.Boolean")) {
                        // 判断符合哪条评分类型
                        if (name.equals(item.getScoreStandard())) {
                            // 判断符合哪个判断
                            if ((Boolean) value == item.getScoreTinyint()) {
                                score = score + item.getScoreValue();
                            }
                        }
                    } else if (typeName.equals("java.lang.Double")) {
                        // 判断符合哪条评分类型
                        if (name.equals(item.getScoreStandard())) {
                            double m = (double) value;
                            if (m == item.getScoreDouble()) {
                                score = score + item.getScoreValue();
                            }
                        }
                    }
                }

            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return score;
    }
}
